<template>
  <div class="login-container">
    <div class="login-box">
      <!-- 头像盒子 -->
      <div class="avatar-box">
        <img src="../assets/logo.png" />
      </div>
      <!-- 表单区域 -->
      <!-- :model='form'存储表单数据 -->
      <!-- rules='rules'存储定义表单的校验规则 -->
      <el-form ref="form" class="login_form" :model="form" :rules="rules">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            prefix-icon="iconfont icon-user"
            v-model="form.username"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            prefix-icon="iconfont icon-3702mima"
            v-model="form.password"
            type="password"
          ></el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="btns">
          <el-button @click="login" type="primary">登录</el-button>
          <el-button @click="reserForm" type="info">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data () {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 2, max: 6, message: '长度在 2 到 6 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created () { },
  methods: {
    // 点击了重置
    reserForm () {
      this.$refs.form.resetFields()
    },
    // 点击登录
    login () {
      this.$refs.form.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$axios.post('login', this.form)
        if (res.meta.status !== 200) return this.$message.error('输入的不正确')
        this.$message.success('登录成功')
        console.log(res)
        // 登录成功将token存储到sessionStorage中
        sessionStorage.setItem('token', res.data.token)
        // 利用编程时导航跳转到home首页
        this.$router.push('/home')
      })
    }
  }
}
</script>
<style scoped lang="less">
.login-container {
  position: relative;
  width: 100%;
  height: 100%;
  background-color: #2b4b6b;
  .login-box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 5px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .avatar-box {
      width: 130px;
      height: 130px;
      border-radius: 50%;
      border: 1px solid #eee;
      padding: 10px;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      background: #fff;
      box-shadow: 0px 0px 10px pink;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
      }
    }
  }
  .login_form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
    .btns {
      float: right;
    }
  }
}
</style>
